package com.example.excel;

import java.util.ArrayList;
import java.util.List;

import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.style.TableStyle;
import com.deepoove.poi.policy.MiniTableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import com.deepoove.poi.policy.DynamicTableRenderPolicy;

public class CwTableRenderPolicy extends DynamicTableRenderPolicy {

    /**
     * 开始行的索引（由于该申报书各部分表格都是整体表格，所以使用索引需要考虑整体的行数和列数），
     * 最终可能行数是动态的，需要根据其上面的数据的条数加上固定行数的索引来记做起始索引
     */
    private int startRowPos = 0;

    /**
     * @param startRowPos 开始行索引,表格上面有动态内容需要考虑加和
     */
    public CwTableRenderPolicy(int startRowPos) {
        this.startRowPos = startRowPos;
    }

    @Override
    public void render(XWPFTable table, Object data) {
        List<String> list = (List<String>) data;
        //模版行

        XWPFTableRow tplRow = table.getRow(startRowPos);
        CTRow ctRow = CTRow.Factory.newInstance();
        ctRow.set(tplRow.getCtRow().copy());
        XWPFTableRow row = new XWPFTableRow(ctRow, table);
        //table.removeRow(startRowPos);
        for (int i = 0; i < list.size(); i++) {

/*           String cw = list.get(i);
            //未保证表格样式，从模版行拷贝配置，重新设置数据
            CTRow ctRow = CTRow.Factory.newInstance();
            ctRow.set(tplRow.getCtRow().copy());
            XWPFTableRow row = new XWPFTableRow(ctRow, table);*/
            //table.removeRow(startRowPos);
            //名称
           // row.getCell(0).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            //值
            //  row.getCell(1).setText(cw);
           XWPFTableRow insertNewTableRow = table.insertNewTableRow(i);
         //   insertNewTableRow.getCtRow().setTrPr(row.getCtRow().getTrPr());
            XWPFTableCell cell = insertNewTableRow.createCell();
            cell.setWidth("200");
            cell.setText("1111111111");
            insertNewTableRow.createCell().setText(Integer.toString(i));
            insertNewTableRow.createCell();
            insertNewTableRow.createCell();
            //row.createCell();
           // table.addRow(row);
         //   table.addRow(row, startRowPos + i + 1);
            RowRenderData build = RowRenderData.build("");
         /*   TableStyle  rowStyle = new TableStyle();
            rowStyle.setAlign(STJc.CENTER);
            build.setRowStyle(rowStyle);*/
            MiniTableRenderPolicy.Helper.renderRow(table, i,build);

            //
        }
        table.removeRow(list.size());


        TableTools.mergeCellsVertically(table, 0,0 , list.size()-1);
        for (int i = 0; i < list.size()-1; i++) {
            XWPFTableCell cell = table.getRow(i).getCell(0);
            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            cell.setWidth("15");
        }

        }



        public int getStartRowPos () {
            return startRowPos;
        }

        public void setStartRowPos ( int startRowPos){
            this.startRowPos = startRowPos;
        }

    }
